package com.canbot.u05.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.adapter.RecordAdapter;
import com.canbot.u05.adapter.RecordAdapter.OnDeleteListener;
import com.canbot.u05.adapter.RecordAdapter.OnPlayListener;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.view.CenterDialog;
import com.canbot.u05.view.CenterDialogOne;
import com.canbot.u05.view.IButtonClickListerner;

/**
 * 自定义待机语音界面
 *
 * @author ruandan
 */
public class StandbyVoiceActivity extends BaseActivityNoTitle
        implements OnClickListener {

        private RelativeLayout rlCreateVoice;

        private ListView lvRecord;

        private ImageView ivAdd;

        private CheckBox cbSwitch;

        private RelativeLayout rlDisable;

        private Button btnBack;

        private CenterDialog deleteDialog;

        private CenterDialogOne fullDialog;

        private RecordAdapter adapter;

        private View prompt;

        private int promptTime = 0;

        private int pos;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_standby_voice);
                initView();
        }

        @Override
        protected void onResume() {
                super.onResume();
                if (adapter != null) {
                        adapter.notifyDataSetChanged();
                }
        }

        private void initView() {
                rlCreateVoice = (RelativeLayout) findViewById(R.id.rlCreateVoice);
                rlDisable = (RelativeLayout) findViewById(R.id.rlDisable);
                btnBack = (Button) findViewById(R.id.btn_head_back);
                lvRecord = (ListView) findViewById(R.id.lvRecord);
                ivAdd = (ImageView) findViewById(R.id.ivAdd);
//              cbSwitch =(CheckBox) findViewById(R.id.cbSwitch);
                prompt = (View) findViewById(R.id.prompt);
                // 提示界面只播放一次
                SharedPreferences sp = getSharedPreferences("Prompt",
                        Context.MODE_PRIVATE);
                String isPlayed = sp.getString("isPlayed", "");
                if ("yes".equals(isPlayed)) {
                        prompt.setVisibility(View.GONE);
                }
                ivAdd.setOnClickListener(this);
                prompt.setOnClickListener(this);
                btnBack.setOnClickListener(this);
//                cbSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//                        @Override
//                        public void onCheckedChanged(CompoundButton buttonView,
//                                                     boolean isChecked) {
//                                if (isChecked) {
//                                        rlCreateVoice.setVisibility(View.VISIBLE);
//                                        rlDisable.setVisibility(View.GONE);
//                                }
//                                else {
//                                        rlCreateVoice.setVisibility(View.GONE);
//                                        rlDisable.setVisibility(View.VISIBLE);
//                                }
//
//                        }
//                });
                adapter = new RecordAdapter(this,
                        ((MainApplication) getApplication()).mStandbyVoices);
                adapter.SetOnDeleteListener(new OnDeleteListener() {
                        SharedPreferences share = getSharedPreferences(
                                ConstUtils.CUSTOM_VOICE, MODE_PRIVATE);

                        SharedPreferences.Editor edit = share.edit(); // 编辑文件

                        @Override
                        public void onDelete(ImageView ivDelete, final int position) {
                                deleteDialog = new CenterDialog(StandbyVoiceActivity.this,
                                        R.style.centerdialog, "确定要删除选中的语音吗？",
                                        new IButtonClickListerner() {

                                                @Override
                                                public void doBtnRight() {
                                                        // 发送数据到优友本机
                                                        MsgSendUtils.sendStringMsg(
                                                                MsgType.DELETE_STANDBY_VOICE,
                                                                ((MainApplication) getApplication()).mStandbyVoices
                                                                        .get(position).getContent());
                                                        //删除本地数据
                                                        ((MainApplication) getApplication()).mStandbyVoices
                                                                .remove(position);
                                                        adapter.notifyDataSetChanged();
                                                        String contents = JSON.toJSONString(
                                                                ((MainApplication) getApplication()).mStandbyVoices);
                                                        edit.putString(ConstUtils.STANDBY_VOICE_CONTENT,
                                                                contents);
                                                        edit.apply();

                                                }

                                                @Override
                                                public void doBtnLeft() {
                                                        deleteDialog.dismiss();
                                                }
                                        });
                                deleteDialog.show();

                        }
                });
                adapter.SetOnPlayListener(new OnPlayListener() {

                        @Override
                        public void onPlay(ImageView ivPlay, int position) {
                                if (ConstUtils.VOICE_TYPE_FILE == ((MainApplication) getApplication()).mStandbyVoices
                                        .get(position).getType()) {
//                                        mSound.playSound(
//                                                ((MainApplication) getApplication()).mStandbyVoices
//                                                        .get(position).getContent(),
//                                                null);

                                }
                                else if (ConstUtils.VOICE_TYPE_TTS == ((MainApplication) getApplication()).mStandbyVoices
                                        .get(position).getType()) {
//                                        mSound.playTTs(
//                                                ((MainApplication) getApplication()).mStandbyVoices
//                                                        .get(position).getContent());
                                }

                        }
                });
                lvRecord.setAdapter(adapter);
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.ivAdd:
                                if (((MainApplication) getApplication()).mStandbyVoices
                                        .size() >= 10) {
                                        fullDialog = new CenterDialogOne(StandbyVoiceActivity.this,
                                                R.style.centerdialog, "声控对话数量已达上限",
                                                new IButtonClickListerner() {

                                                        @Override
                                                        public void doBtnRight() {
                                                                fullDialog.dismiss();

                                                        }

                                                        @Override
                                                        public void doBtnLeft() {
                                                                // TODO Auto-generated method stub

                                                        }
                                                });
                                        fullDialog.show();
                                        return;
                                }
                                Intent intent = new Intent(StandbyVoiceActivity.this,
                                        RecordActivity.class);
                                intent.putExtra("fromActivity", ConstUtils.ACTIVITY_STANDBY_VOICE);
                                startActivity(intent);

                                break;
                        case R.id.btn_head_back:
                                this.finish();
                                break;

                        case R.id.prompt:
                                // 提示界面只播放一次
                                SharedPreferences sp = getSharedPreferences("Prompt",
                                        Context.MODE_PRIVATE);
                                String isPlayed = sp.getString("isPlayed", "");
                                if (!"yes".equals(isPlayed)) {
                                        if (promptTime <= 0) {
                                                prompt.setBackgroundResource(
                                                        R.drawable.standby_prompt2);
                                                promptTime++;
                                        }
                                        else {
                                                prompt.setVisibility(View.GONE);
                                                Editor editor = sp.edit();
                                                editor.putString("isPlayed", "yes");
                                                editor.apply();
                                        }
                                }

                                break;
                }

        }

}
